package cn.wolfcode.wms.util;

import lombok.Getter;

import java.util.Collections;
import java.util.List;

@Getter
public  class PageResult {

    public static final PageResult EMPRY_PAGE = new PageResult(Collections.EMPTY_LIST,0,1,1);

    //数据库查询出来的
    private List<?> data;
    private Integer rows;

    //用户操作的页数
    private int currentPage;
    private int pageSize;

    //帮前台计算好的数据
    private int endPage;
    private int prevPage;
    private int nextPage;

    public PageResult(List<?> data, Integer rows, int currentPage, int pageSize) {
        this.data = data;
        this.rows = rows;
        this.currentPage = currentPage;
        this.pageSize = pageSize;

        //当总记录数小于分页数时
        if(rows < pageSize){
            endPage = 1;
            prevPage = 1;
            nextPage = 1;
        }

        endPage = rows % pageSize == 0 ? rows / pageSize : rows / pageSize +1;
        prevPage = currentPage > 1 ? currentPage - 1 : 1 ;
        nextPage = currentPage >= endPage ? endPage : currentPage + 1;
    }
}
